package com.yss.reportworld.controller;

import java.io.IOException;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.constant.RequestDataHelper;
import com.yss.common.core.exception.BusinessException;
import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.reportworld.domain.*;
import com.yss.reportworld.domain.dim.DimZblx;
import com.yss.reportworld.service.*;
import com.yss.reportworld.util.TableInfoUtil;
import org.springframework.validation.annotation.Validated;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import org.springframework.web.multipart.MultipartFile;

/**
 * 指标类型Controller
 *
 * @author yss
 * @date 2023-05-12
 */
@RestController
@RequestMapping("/dimZblx")
public class DImZblxController extends BaseController {
    @Autowired
    private IDimZblxService IDimZblxService;
    @Autowired
    private ISqlScriptService sqlScriptService;

    /**
     * 查询指标类型列表
     */
    @RequiresPermissions("reportworld:zblx:list")
    @GetMapping("/list")
    public AjaxResult list(DimZblx dimZblx) {
        List<DimZblx> list = IDimZblxService.selectDimZblxList(dimZblx);
        return success(list);
    }

    /**
     * 导出指标类型列表
     */
    @RequiresPermissions("reportworld:zblx:export")
    @Log(title = "指标类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DimZblx dimZblx) {
        List<DimZblx> list = IDimZblxService.selectDimZblxList(dimZblx);
        EasyExeclUtil.export(response, list, DimZblx.class);
    }

    /**
     * 导出模板
     *
     * @param response
     * @throws IOException
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        EasyExeclUtil.export(response, null, DimZblx.class);
    }

    /**
     * 初始化 加载
     */
    @RequiresPermissions("reportworld:report:list")
    @Log(title = "报表信息", businessType = BusinessType.OTHER)
    @PostMapping("/init")
    public AjaxResult init(@RequestBody Report report) {
        try {
            TableInfoUtil.handleTableVersion(report.getHisVersion()); // 替换表名
            AjaxResult ajax = AjaxResult.success();
            ajax.put("hisVersions", sqlScriptService.getReductionVersion(false));
            return ajax;
        } catch (Exception ex) {
            throw new BusinessException("初始化加载数据出错，请稍后重试！" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    /**
     * 获取指标类型详细信息
     */
    @RequiresPermissions("reportworld:zblx:query")
    @GetMapping(value = "/getZblx")
    public AjaxResult getZblx(@RequestBody DimZblx dimZblx) {
        return success(IDimZblxService.getById(dimZblx.getId()));
    }

    /**
     * 获取指标类型详细信息
     */
    @RequiresPermissions("reportworld:zblx:query")
    @PostMapping(value = "/getDimZblxByCode")
    public AjaxResult getDimZblxByCode(@RequestBody DimZblx dimZblx) {
        QueryWrapper<DimZblx> wrapper = new QueryWrapper<>();
        wrapper.eq("FINDX_TP_CODE", dimZblx.getFindxTpCode());
        return success(IDimZblxService.getOne(wrapper));
    }

    /**
     * 新增指标类型
     */
    @RequiresPermissions("reportworld:zblx:add")
    @Log(title = "指标类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated DimZblx dimZblx) {
        if (!IDimZblxService.checkDimZblxCodeeUnique(dimZblx)) {
            return error("指标编码【" + dimZblx.getFindxTpCode() + "】已存在");
        }
        dimZblx.setId(IdUtils.fastSimpleUUID());
        dimZblx.setCreateBy(SecurityUtils.getUsername());
        dimZblx.setCreateTime(new Date());
        return toAjax(IDimZblxService.save(dimZblx));
    }

    /**
     * 修改指标类型
     */
    @RequiresPermissions("reportworld:zblx:edit")
    @Log(title = "指标类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated DimZblx dimZblx) {
        if (!IDimZblxService.checkDimZblxCodeeUnique(dimZblx)) {
            return error("指标编码【" + dimZblx.getFindxTpCode() + "】已存在");
        }
        dimZblx.setUpdateTime(new Date());
        dimZblx.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(IDimZblxService.updateById(dimZblx));
    }

    /**
     * 删除指标类型
     */
    @RequiresPermissions("reportworld:zblx:remove")
    @Log(title = "指标类型", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody DimZblx dimZblx) {
        QueryWrapper<DimZblx> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID", dimZblx.getId());
        List<DimZblx> list = IDimZblxService.list(wrapper);
        if (list.size() > 0) {
            return warn("存在下级指标,不允许删除");
        }
        IDimZblxService.removeDimZblx(dimZblx.getId());
        return success();
    }


    /**
     * 读取需求BUG信息
     */
    @RequiresPermissions("reportworld:zblx:import")
    @Log(title = "需求BUG信息", businessType = BusinessType.IMPORT)
    @PostMapping("/readDimZblxInfo")
    public AjaxResult readDimZblxInfo(MultipartFile file, boolean updateSupport, String hisVersion) throws Exception {
        return success(IDimZblxService.readDimZblxInfo(file, updateSupport, hisVersion));
    }
}
